package com.xzq.erp.controller;


import com.xzq.erp.aspect.ApiOperationLog;
import com.xzq.erp.domain.dto.DeleteDTO;
import com.xzq.erp.domain.dto.PageDTO;
import com.xzq.erp.domain.dto.SalesOpportunityDTO;
import com.xzq.erp.domain.query.CustomerPageQuery;
import com.xzq.erp.domain.query.SalesOpportunityPageQuery;
import com.xzq.erp.domain.vo.CustomerVO;
import com.xzq.erp.domain.vo.SalesOpportunityVO;
import com.xzq.erp.service.ISalesOpportunityService;
import com.xzq.erp.utils.Response;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.NotEmpty;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 销售机会表 前端控制器
 * </p>
 *
 * @author xzq
 * @since 2025-05-13
 */
@RestController
@RequestMapping("/sales-opportunity")
@RequiredArgsConstructor
@Tag(name = "销售机会相关接口")
public class SalesOpportunityController {

    private final ISalesOpportunityService salesOpportunityService;

    /**
     * 分页查询销售机会列表
     */
    @GetMapping("/list")
    @Operation(summary = "分页获取销售机会列表")
    @ApiOperationLog(description = "分页获取销售机会列表")
    @PreAuthorize("hasAuthority('salesOpportunity:list') || hasRole('ADMIN')")
    public Response<PageDTO<SalesOpportunityVO>> getSalesOpportunityList(@ParameterObject @Validated SalesOpportunityPageQuery query) {
        return Response.success(salesOpportunityService.getSalesOpportunityList(query));
    }

    /**
     * 新增
     */
    @PostMapping
    @Operation(summary = "新增销售机会")
    @ApiOperationLog(description = "新增销售机会")
    @PreAuthorize("hasAuthority('salesOpportunity:add') || hasRole('ADMIN')")
    public Response<Void> add(@Validated @RequestBody SalesOpportunityDTO salesOpportunityDTO) {
        salesOpportunityService.save(salesOpportunityDTO);
        return Response.success();
    }

    /**
     * 修改
     */
    @PutMapping
    @Operation(summary = "修改销售机会")
    @ApiOperationLog(description = "修改销售机会")
    @PreAuthorize("hasAuthority('salesOpportunity:update') || hasRole('ADMIN')")
    public Response<Void> update(@Validated @RequestBody SalesOpportunityDTO salesOpportunityDTO) {
        salesOpportunityService.update(salesOpportunityDTO);
        return Response.success();
    }

    /**
     * 批量删除
     */
    @DeleteMapping
    @Operation(summary = "批量删除销售机会")
    @ApiOperationLog(description = "批量删除销售机会")
    @PreAuthorize("hasAuthority('salesOpportunity:delete') || hasRole('ADMIN')")
    public Response<Void> delete(@RequestBody List<Long> ids) {
        salesOpportunityService.removeBatchByIds(ids);
        return Response.success();
    }

    /**
     * 批量删除
     */
    @DeleteMapping("/wx")
    @Operation(summary = "批量删除销售机会")
    @ApiOperationLog(description = "批量删除销售机会")
    @PreAuthorize("hasAuthority('salesOpportunity:delete') || hasRole('ADMIN')")
    public Response<Void> wxDelete(@RequestBody @Validated DeleteDTO deleteDTO) {
        salesOpportunityService.removeBatchByIds(deleteDTO.getIds());
        return Response.success();
    }
}
